/**
 * Created by xc on 16/08/20.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbBrainMindModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`bm_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '脑图id',
  //`bm_type` int(1) NOT NULL DEFAULT '0' COMMENT '脑图类型',
  //`user_id` bigint(20) NOT NULL COMMENT '用户id',
  //`data` mediumtext COMMENT '脑图数据',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbBrainMindModel.prototype = new ItbBaseModel();

// 取得表名
ItbBrainMindModel.prototype.getTableName = function(){
    return 'itb_brain_mind';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbBrainMindModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'bm_id='+this.bm_id;
}


// 根据ids批量查找graph
// 参数：ids (逗号分隔的graph_id)
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbBrainMindModel.prototype.getLatestByUserId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbBrainMindModel.getLatestByUserId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT * FROM itb_brain_mind t ' +
            ' WHERE t.del_flg = 0 ' +
            ' AND t.user_id = ' + THIS.user_id +
            ' ORDER BY t.update_time DESC,t.bm_id DESC limit 1 ';
        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            var retRecd = null;
            if(result && result.length>0){
                retRecd = result[0];
            }

            callback(error, retRecd);
        });
    });
}

// 导出
module.exports = ItbBrainMindModel;